import sys
import math
import typing
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from EnemySpark import EnemySpark


class Bullet(QGraphicsItem):
    def __init__(self, scene):
        super(Bullet, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()

        self.setVisible(False)
        self.speed = 8
        self.speedx = 0
        self.speedy = 0
        self.damage = 200  # 子弹伤害

        self.pix = QPixmap(self.root + "/res/_bullet.png").scaled(18, 9)
        self.scene = scene
        self.scene.addItem(self)

        self.pix_map = []
        for i in range(5):
            self.pix_map.append(QPixmap(self.root + "/res/smoke_0" + str(i + 1) + ".png"))
        self.Times = 0
        self.Symbol = False

        self.EnemySpark = EnemySpark(self.scene)

    def boundingRect(self):
        return QRectF(-self.pix.width() * 0.5, -self.pix.height() * 0.5, self.pix.width(), self.pix.height())

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):

        if 50 >= self.Times > 0:
            x = self.Times % 5
            painter.drawPixmap(-self.pix_map[x].width() * 0.5, -self.pix_map[x].height() * 0.5, self.pix_map[x])
            self.Times = self.Times + 1

        elif self.Times == 0:
            painter.drawPixmap(-self.pix.width() * 0.5, -self.pix.height() * 0.5, self.pix)
        else:
            self.setVisible(False)
            self.Times = 0

    def CallSmoke(self):
        self.Times = 1

    def advance(self, phase: int):
        # self.setPos(QPointF(self.mapToScene(0,0).x()+self.speedx,self.mapToScene(0,0).y()+self.speedy))
        if self.isVisible():
            if self.mapToScene(0, 0).x() < 0 or self.mapToScene(0, 0).y() < 0 or \
                    self.mapToScene(0, 0).x() > self.scene.width() or self.mapToScene(0, 0).y() > self.scene.height():
                self.setVisible(False)
            elif self.Times == 0:
                self.setPos(self.mapToScene(self.speed, 0))
                self.collidingItems()
            elif self.Times <= 50 and self.Times > 0:
                self.setPos(self.mapToScene(0, 0))

    def setPos(self, pos: typing.Union[QPointF, QPoint]):
        super(Bullet, self).setPos(pos)

    def setRotation(self, angle: float):
        super(Bullet, self).setRotation(angle)

    def setVisible(self, visible: bool):
        super(Bullet, self).setVisible(visible)

    def isVisible(self):
        return super(Bullet, self).isVisible()
